server := qrcode
client := client
outdir := .
AS_PATH := ../..

#outair := ../../air
# $(patsubst %.c,%.o,$(wildcard *.c))
file:=qrcode
filename:=qrcode
client_air := $(outair)/$(client).air
server_air := $(outair)/$(server).air
ASSET:= $(foreach x,${outdir}, $(notdir $(wildcard $(addprefix  ${x}/*,*))))

WIN_ANE := windows.ane
ANDROID_ANE := android.ane
server_app_xml := $(outdir)/$(server)-app.xml
client_app_xml := $(outdir)/$(client)-app.xml
AS_SERVER := $(server).as
SERVER_PATH := .#.as文件所在目录
CLIENT_PATH := .#.as文件所在目录
#得到该目录下所有的 .as 文件
SERVER_AS := $(foreach x,${SERVER_PATH}, $(wildcard $(addprefix  ${x}/*,.as) ) ) 
CLIENT_AS := $(foreach x,${CLIENT_PATH}, $(wildcard $(addprefix  ${x}/*,.as) ) ) 

AS_SERVER_FILES:= SwfLoader.as MyTcpServer.as
AS_CLIENT := $(client).as
SWC_CLASS := MyExtension
SWC_CLASS_PATH := .
DLL := extension/my.dll
SO := extension/libmy.so

ifeq ($(OS),Windows_NT)
FLEX_HOME := d:/flex_sdk_4.5
NDK_HOME := d:/android-ndk-r7
else
FLEX_HOME := /home/libiao/flex_sdk_4.5
NDK_HOME := /home/libiao/android-ndk-r7
endif
ADL := $(FLEX_HOME)/bin/adl
ADT := $(FLEX_HOME)/bin/adt
AMXMLC := $(FLEX_HOME)/bin/amxmlc
ACOMPC := $(FLEX_HOME)/bin/acompc

ANDROID_ANE_XML:= androidane.xml
rIN_ANE_XML:= windowsane.xml

CC     := $(NDK_HOME)/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/arm-linux-androideabi-gcc
#INCLUDE:= -I$(NDK_HOME)/platforms/android-8/arch-arm/usr/include
#mv $(NDK_HOME)/platforms/android-8/arch-arm/usr/include $(NDK_HOME)/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/lib/gcc/arm-linux-androideabi/4.4.3/include
#LIB    := -L$(NDK_HOME)/platforms/android-8/arch-arm/usr/lib
# mv $(NDK_HOME)/platforms/android-8/arch-arm/usr/lib/* toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/lib/gcc/arm-linux-androideabi/4.4.3/* 

SWC:= ../../lib/greensock.swc,./effect.swc
SERVER_SWF:= $(outdir)/$(server).swf
CLIENT_SWF:= $(outdir)/$(client).swf
CERT:= my.p12
PASSWORD:= db0@qq.com

RM     := rm -f
LINKOBJ:= test.c
#LINKOBJ:= $(patsubst %.c,%.o,$(wildcard *.c)) 


all:$(server_air) #$(client_air) $(server_air)
#	run $(ADL) $(client_app_xml)
	$(ADL) -profile extendedDesktop $(server_app_xml)
	@echo "ok"

$(server_air): $(SERVER_SWF) $(server_app_xml) $(CERT)
#	$(ADT) -package -storetype pkcs12 -keystore $(CERT) -storepass $(PASSWORD) $(server_air) $(server_app_xml) -C $(outdir) $(ASSET)

#	$(ADT) -package -storetype pkcs12 -keystore $(CERT) -storepass $(PASSWORD) -target bundle $(server_air) $(server_app_xml) -extdir . $(SERVER_SWF)
#	$(ADT) -package -storetype pkcs12 -keystore $(CERT) -storepass $(PASSWORD) -target native $(EXE) $(server_app_xml) $(SERVER_SWF)
#	$(ADT) -package -storetype pkcs12 -keystore $(CERT) -tsa none -storepass $(PASSWORD) -target native $(EXE) $(server_app_xml) -extdir . $(SERVER_SWF)
	@echo $(server_air) "is ok"

$(client_air): $(CLIENT_SWF) $(CERT) $(client_app_xml)
#	$(ADT) -package -storetype pkcs12 -keystore $(CERT) -storepass $(PASSWORD) $(client_air) $(client_app_xml) $(CLIENT_SWF) $(ASSET)
#	$(ADT) -package -target apk -storetype pkcs12 -keystore $(CERT) -storepass $(PASSWORD) $(client_air) $(server_app_xml) $(SERVER_SWF)
#	$(ADT) -package -target apk-captive-runtime -storetype pkcs12 -keystore $(CERT) -storepass $(PASSWORD) $(client_air) $(server_app_xml)  -extdir . $(SERVER_SWF) $(ASSET)
#	$(ADL) -profile mobileDevice $(server_app_xml)
#	adb install $(client_air)
#	adb shell am start -n air.$(server)/.AppEntry
#	$(ADL) -extdir android -profile mobileDevice $(server_app_xml)
#	@echo $(client_air) "is ok"

$(CLIENT_SWF):$(CLIENT_AS) $(SWC)
	$(AMXMLC) $(AS_CLIENT) -source-path=$(AS_PATH) -static-link-runtime-shared-libraries=true -debug=true -output $(CLIENT_SWF)

$(SERVER_SWF):$(SERVER_AS)# $(SWC)
	$(AMXMLC) $(AS_SERVER) -source-path=$(AS_PATH) -static-link-runtime-shared-libraries=true -debug=true -output $(SERVER_SWF)
#-library-path+=$(SWC)

$(SWC):$(SWC_CLASS)
	$(ACOMPC) -source-path $(SWC_CLASS_PATH) -include-classes $(SWC_CLASS) -swf-version=14 -output $(SWC)
	rm -rf extension
	mkdir extension
	unzip -o $(SWC) -x catalog.xml && move library.swf extension/library.swf

$(ANDROID_ANE):$(server_app_xml) $(SWC) $(SO)
	$(ADT) -package -target ane $(ANDROID_ANE) $(ANDROID_ANE_XML) -swc $(SWC) -platform Android-ARM -C extension .
	
$(server_app_xml):
	sed -e 's/<id>[^<]*<\/id>/<id>$(server)<\/id>/' -e 's/<content>[^<]*<\/content>/<content>$(server).swf<\/content>/' -e 's/<filename>[^<]*<\/filename>/<filename>$(filename)<\/filename>/' -e 's/<name>[^<]*<\/name>/<name>$(file)<\/name>/' -e 's/<!-- <visible><\/visible> -->/<visible>true<\/visible>/'  -e 's/<!-- <supportedProfiles>[^<]*<\/supportedProfiles> -->/<supportedProfiles>desktop extendedDesktop mobileDevice extendedMobileDevice<\/supportedProfiles>/' $(FLEX_HOME)/templates/air/descriptor-template.xml > $(server_app_xml)

$(client_app_xml):
	sed -e 's/<id>[^<]*<\/id>/<id>$(client)<\/id>/' -e 's/<content>[^<]*<\/content>/<content>$(client).swf<\/content>/' -e 's/<filename>[^<]*<\/filename>/<filename>$(client)<\/filename>/' -e 's/<name>[^<]*<\/name>/<name>$(client)<\/name>/' -e 's/<!-- <visible><\/visible> -->/<visible>true<\/visible>/'  $(FLEX_HOME)/templates/air/descriptor-template.xml > $(client_app_xml)

$(WIN_ANE):$(WIN_ANE_XML) $(SWC)  $(DLL)
	$(ADT) -package -target ane $(WIN_ANE) $(WIN_ANE_XML) -swc $(SWC) -platform Windows-x86  -C extension .

$(SO): $(LINKOBJ)
	$(CC)  -L. -lFlashRuntimeExtensions -shared -o $(SO) $(LINKOBJ)

$(DLL):$(LINKOBJ)
	gcc -shared $(LINKOBJ) -L. -lFlashRuntimeExtensions -o $(DLL)

#$(SWC_CLASS):MyExtension.as
#	echo $<

$(CERT):
	#$(ADT) -certificate -cn db0@qq.com -ou db0@qq.com -o db0@qq.com -c CN -validityPeriod 1 2048-RSA $(CERT) $(PASSWORD)
	
#$(LINKOBJ): %.o: %.c
#	$(CC) -c   $< -o $@


.PHONY : clean
clean:
	$(RM) $(ANDROID_ANE) $(client_air) $(server_air) $(SERVER_SWF) $(WIN_ANE) $(EXE)# $(SWC)  $(DLL) $(SO) win.ane extension $(client_app_xml) $(server_app_xml) *.air *.xml
#$(CERT)
astags:
ifeq ($(OS),Windows_NT)
#	cp $(FLEX_HOME)/bin/swfdump.exe $(FLEX_HOME)/lib/swfdump.exe
#	$(FLEX_HOME)/lib/swfdump.exe -abc $(FLEX_HOME)/frameworks/libs/air/airglobal.swc | grep :[a-zA-Z0-9_]*[\(\/] | sed -e "s/^.*private.*//" | sed -e "s/[^\(\)\/:]*:/:/g" > as3.fun
#	$(FLEX_HOME)/lib/swfdump.exe -abc $(FLEX_HOME)/frameworks/libs/player/10.2/playerglobal.swc | grep "extends.*:.*" > as3.ext
	ctags --options=ctags.txt --fields=+iaS *.fun *.ext *.as
else
	cp $(FLEX_HOME)/bin/swfdump $(FLEX_HOME)/lib/swfdump
#	$(FLEX_HOME)/lib/swfdump.exe -abc $(FLEX_HOME)/frameworks/libs/player/10.2/playerglobal.swc | grep ":.*\/.*:*" | sed -e "s/^.*private.*//" | sed -e "s/[^\(\)\/:]*:/:/g" > as3.fun
	$(FLEX_HOME)/lib/swfdump -abc $(FLEX_HOME)/frameworks/libs/air/airglobal.swc | grep :[a-zA-Z0-9_]*[\(\/] | sed -e "s/^.*private.*//" | sed -e "s/[^\(\)\/:]*:/:/g" > as3.fun
	$(FLEX_HOME)/lib/swfdump -abc $(FLEX_HOME)/frameworks/libs/player/10.2/playerglobal.swc | grep "extends.*:.*" > as3.ext
	ctags --options=./ctags.txt --fields=+iaS *.fun *.ext *.as
#	ctags --options=~/.ctags --fields=+iaS *.fun *.ext *.as
endif
testx:
#	$(FLEX_HOME)/lib/swfdump.exe -abc $(FLEX_HOME)/frameworks/libs/player/10.2/playerglobal.swc | grep :[a-zA-Z0-9_]*\(
	$(FLEX_HOME)/lib/swfdump.exe -abc $(FLEX_HOME)/frameworks/libs/player/10.2/playerglobal.swc > abc
	$(FLEX_HOME)/lib/swfdump.exe -abc $(FLEX_HOME)/frameworks/libs/player/10.2/playerglobal.swc | grep setTimeout
	$(FLEX_HOME)/lib/swfdump.exe -abc $(FLEX_HOME)/frameworks/libs/player/10.2/playerglobal.swc | grep "getChildAt"
	$(FLEX_HOME)/lib/swfdump.exe -abc $(FLEX_HOME)/frameworks/libs/player/10.2/playerglobal.swc | grep "ADDED_TO_STAGE"
	$(FLEX_HOME)/lib/swfdump.exe -abc $(FLEX_HOME)/frameworks/libs/player/10.2/playerglobal.swc | grep "CLICK"
testtags:
	ctags --options=./ctags.txt --fields=+iaS *.as
